package services

import (
	"dims/entity"
	"fmt"
	"time"
)

func {{.ClassName}}Create(context *gin.Context) {
	userId, _ := context.Get("userId")
	log.Println(userId)
	var req entity.{{.ClassName}}
    if err := context.ShouldBindJSON(&req);err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg("获取参数失败"))
	} else {
		res := dao.{{.ClassName}}Create(req)
		context.JSON(http.StatusOK, res)
	}
}

func {{.ClassName}}Delete(context *gin.Context) {
	var id = context.Param("id")
    result := model.{{.ClassName}}Delete(id)
    context.JSON(http.StatusOK, result)
}

func {{.ClassName}}DeleteByIds(context *gin.Context) {
    var ids = context.Param("ids")
    result := dao.{{.ClassName}}Delete(ids)
    context.JSON(http.StatusOK, result)
}

func {{.ClassName}}Update(context *gin.Context) {
    var req model.{{.ClassName}}
    if err := context.ShouldBindJSON(&req);err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg("获取参数失败"))
	} else {
		res := dao.{{.ClassName}}Update(req)
		context.JSON(http.StatusOK, res)
	}
}

func Get{{.ClassName}}(context *gin.Context) {
    idstr:=context.Param("id")    //查询路径Path参数
    id, err := strconv.ParseInt(idstr, 10, 64)
    if err!=nil{
        context.JSON(http.StatusOK, R.ReturnFailMsg("获取参数失败"))
    }else{
        res := dao.{{.ClassName}}LoadById(id)
    	context.JSON(http.StatusOK, res)
    }
}


func GetPageLimit{{.ClassName}}(context *gin.Context) {
    pageNum, _ := strconv.Atoi(context.DefaultQuery("pageNum", "1"))
    pageSize, _ := strconv.Atoi(context.DefaultQuery("pageSize", "10"))

    beginTime := context.DefaultQuery("params[beginTime]", "")
    endTime := context.DefaultQuery("params[endTime]", "")

	var req entity.{{.ClassName}}PageReq
	// 将查询参数绑定到结构体
	if err := context.ShouldBindQuery(&req); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
    var param = tools.SearchTableDataParam{
        PageNum:  pageNum,
        PageSize: pageSize,
        Other: req,
        Params: tools.Params{
            BeginTime: beginTime,
            EndTime:   endTime,
        },
    }

    result := dao.GetPageLimit{{.ClassName}}(param)
    context.JSON(http.StatusOK, result)
}

func GetListLimit{{.ClassName}}(context *gin.Context) {


    beginTime := context.DefaultQuery("params[beginTime]", "")
    endTime := context.DefaultQuery("params[endTime]", "")

	var req entity.{{.ClassName}}PageReq
	// 将查询参数绑定到结构体
	if err := context.ShouldBindQuery(&req); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
    var param = tools.SearchTableDataParam{
        PageNum:  0,
        PageSize: 0,
        Other: req,
        Params: tools.Params{
            BeginTime: beginTime,
            EndTime:   endTime,
        },
    }

    result := dao.GetListLimit{{.ClassName}}(param)
    context.JSON(http.StatusOK, result)
}